package com.lzxm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lzxm.dao.SxDao;
import com.lzxm.po.SxInfo;
import com.lzxm.po.SxInfoVo;
import com.lzxm.po.SxType;
import com.lzxm.constant.DateUtil;

@Service
public class SxService {

	@Autowired
	private SxDao sxDao;
	@Autowired
	private UserService userService;
	
	public List<SxType> selectAllType(){
		return sxDao.selectAllType();
	}
	
	public String selectStatusById(int id){
		return sxDao.selectStatusById(id);
	}
	
	public void deleteSxById(int id){
		sxDao.deleteSxById(id);
	}
	
	//按条件查询
	public List<SxInfoVo> selectSxInfo(String sx_name){
		List<SxInfo> lists = sxDao.selectSxInfo(sx_name);
		
		return assumeSxInfoVo(lists);
	}
	
	
	private List<SxInfoVo> assumeSxInfoVo(List<SxInfo> lists){
		List<SxInfoVo> result = new ArrayList<SxInfoVo>();
		for(int i = 0; i < lists.size(); i++){
			SxInfoVo temp = new SxInfoVo();
			temp.setSx_id(lists.get(i).getSx_id());
			temp.setSx_name(lists.get(i).getSx_name());
			temp.setSx_enterprice(lists.get(i).getSx_enterprice());
			temp.setSx_found_timestape(DateUtil.dateToStr(lists.get(i).getSx_found_timestape()));
			//
			if(lists.get(i).getSx_type_id() == 1){
				temp.setSx_type("区级督察事项");
			}
			else if(lists.get(i).getSx_type_id() == 2){
				temp.setSx_type("市级督察事项");
			}
			else if(lists.get(i).getSx_type_id() == 3){
				temp.setSx_type("重点项目");
			}
			else{
				temp.setSx_type("中心工作");
			}
			
			//
			if(lists.get(i).getSx_status_id() == 0){
				temp.setSx_status("等待领导审批");
			}
			else if(lists.get(i).getSx_status_id() == 1){
				temp.setSx_status("审批通过");
			}
			else if(lists.get(i).getSx_status_id() == 2){
				temp.setSx_status("审批不通过");
			}
			else if(lists.get(i).getSx_status_id() == 3){
				temp.setSx_status("已注销");
			}
			String name = userService.getUsernameById(lists.get(i).getSx_founder_id());
			temp.setSx_founder(name);
			
			result.add(temp);
		}
		return result;
	}
	
	public void insertSx(SxInfo sxInfo){
		sxDao.insertSx(sxInfo);
	}
	
	public void updateSx(SxInfo sxInfo){
		sxDao.updateSX(sxInfo);
	}
	
	public List<SxInfoVo> getByType(int type_id){
		List<SxInfo> lists = sxDao.getByType(type_id);
		return assumeSxInfoVo(lists);
	}
}
